package com.mf.morefun.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.mf.morefun.R;
import com.mf.morefun.bean.DoHomeWorkListBean;
import com.mf.morefun.listener.HomeWorkListListener;
import com.mf.morefun.listener.ReadDetailTypeListener;
import com.mf.morefun.widget.TransformationItemUtils;
import com.zhcx.zhcxlibrary.utils.DateUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import org.jetbrains.annotations.Nullable;

import java.math.MathContext;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static android.support.constraint.Constraints.TAG;

public class HomeWorkListAdapter extends RecyclerView.Adapter<HomeWorkListAdapter.ViewHolder> {
    private Context mContext;
    private List<DoHomeWorkListBean> doHomeWorkListBeanList;
    private HomeWorkListListener listener;
    private LayoutInflater inflater;
    private ReadDetailTypeListener typeListener;

    public ReadDetailTypeListener getTypeListener() {
        return typeListener;
    }

    public void setTypeListener(ReadDetailTypeListener typeListener) {
        this.typeListener = typeListener;
    }

    public HomeWorkListAdapter(Context mContext, List<DoHomeWorkListBean> doHomeWorkListBeanList) {
        this.mContext = mContext;
        this.doHomeWorkListBeanList = doHomeWorkListBeanList;
        inflater = LayoutInflater.from(mContext);
    }

    public HomeWorkListListener getListener() {
        return listener;
    }

    public void setListener(HomeWorkListListener listener) {
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_homeworklist, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        try {
            holder.tv_createdate.setText(new SimpleDateFormat("MM-dd").format(DateUtils.DATE_YEAR_MONTH_DAY.parse(doHomeWorkListBeanList.get(position).getStartDate())));
        }catch (Exception e){
            e.printStackTrace();
            holder.tv_createdate.setText(new SimpleDateFormat("MM-dd").format(new Date(System.currentTimeMillis())));
        }
        //3为录音  4为视频， 5为创作
        if (null != doHomeWorkListBeanList.get(position).getMfTpLRR()){
            if (!StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpLRR().getType())){
                if (doHomeWorkListBeanList.get(position).getMfTpLRR().getType().equals("3")){
                    holder.tv_music.setText("录音");
                }else if(doHomeWorkListBeanList.get(position).getMfTpLRR().getType().equals("4")){
                    holder.tv_music.setText("视频");
                }else {
                    holder.tv_music.setText("创作");
                }
                holder.tv_title.setText(doHomeWorkListBeanList.get(position).getMfTpLRR().getName());
                String url = "";
                if (null != doHomeWorkListBeanList.get(position).getMfTpLRR().getBook()){
                    url = doHomeWorkListBeanList.get(position).getMfTpLRR().getBook().getCover();
                }else {
                    if (null != doHomeWorkListBeanList.get(position).getMfTpCreation()){
                        url = doHomeWorkListBeanList.get(position).getMfTpCreation().getPicDescription();
                    }else {
                        url = "";
                    }
                }
                //设置图片圆角角度
                RoundedCorners roundedCorners = new RoundedCorners(1);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(158, 130);
                Glide.with(mContext).load(url).listener(new mRequestListener(holder)).apply(options).into(holder.iv_image);
                if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpComment())){
                    holder.llComment.setVisibility(View.GONE);
                }else {
                    holder.llComment.setVisibility(View.VISIBLE);
                }
            }
        }else {
            if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpComment())){
                holder.llComment.setVisibility(View.GONE);
            }else {
                holder.llComment.setVisibility(View.VISIBLE);
            }
            if (null != doHomeWorkListBeanList.get(position).getMfTpLRR()) {
                holder.tv_title.setText(doHomeWorkListBeanList.get(position).getMfTpLRR().getName());
            } else if (null != doHomeWorkListBeanList.get(position).getMfTpCreation()) {
                holder.tv_title.setText(doHomeWorkListBeanList.get(position).getMfTpCreation().getName());
            } else {
                holder.tv_title.setText("");
            }
            if (null != doHomeWorkListBeanList.get(position).getMfTpCreation()) {
                holder.tv_music.setText("创作");
                //设置图片圆角角度
                RoundedCorners roundedCorners = new RoundedCorners(1);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(158, 130);
                Glide.with(mContext).load(doHomeWorkListBeanList.get(position).getMfTpCreation().getPicDescription()).listener(new mRequestListener(holder)).apply(options).into(holder.iv_image);
            } else {
                if (null != doHomeWorkListBeanList.get(position).getMfTpLRR()) {
                    //设置图片圆角角度
                    RoundedCorners roundedCorners = new RoundedCorners(1);
                    RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(158, 130);
                    Glide.with(mContext).load(doHomeWorkListBeanList.get(position).getMfTpLRR().getBook().getCover()).listener(new mRequestListener(holder)).apply(options).into(holder.iv_image);
                    if (doHomeWorkListBeanList.get(position).getMfTpLRR().getType().equals("1")) {
                        holder.tv_music.setText("磨耳朵");
                    } else if (doHomeWorkListBeanList.get(position).getMfTpLRR().getType().equals("2")) {
                        holder.tv_music.setText("阅读");
                    } else {
                        holder.tv_music.setText("录音");
                    }
                } else {
                    holder.tv_music.setText("磨耳朵");
                }
            }
        }
        //计算时间
        caluateDate(holder, DateUtils.DATE_YEAR_MONTH_DAY.format(new Date(System.currentTimeMillis())), doHomeWorkListBeanList.get(position).getEndDate());
        if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpRecordFlag())){
            holder.iv_type.setImageResource(R.mipmap.weiwancheng);
            holder.tv_do.setBackgroundResource(R.drawable.blue_shapes);
        }else {
            if (doHomeWorkListBeanList.get(position).getMfTpRecordFlag().equals("0")) {//
                holder.iv_type.setImageResource(R.mipmap.weiwancheng);
                holder.tv_do.setBackgroundResource(R.drawable.blue_shapes);
            } else {
                holder.iv_type.setImageResource(R.mipmap.wancheng);
                holder.tv_do.setBackgroundResource(R.drawable.gray_shapes);
            }
        }
    }

    private void caluateDate(ViewHolder holder, String startDate, String endDate) {
        if (StringUtils.isEmpty(endDate)) {
            holder.tv_time.setText("已过期");
            return;
        }
        String start[] = startDate.split("-");
        String end[] = endDate.split("-");
        if (start.length != 3) {
            holder.tv_time.setText("已过期");
            return;
        }
        if (end.length != 3) {
            holder.tv_time.setText("已过期");
            return;
        }
        try {
            int year = Integer.parseInt(end[0]) - Integer.parseInt(start[0]);
            int month = Integer.parseInt(end[1]) - Integer.parseInt(start[1]);
            int day = Integer.parseInt(end[2]) - Integer.parseInt(start[2]);
            int time = (year * 365 + month * 30 + day);
            if (time < 0){
                holder.tv_time.setText("已过期");
            }else {
                holder.tv_time.setText((year * 365 + month * 30 + day) + "天");//计算时间
            }
        } catch (Exception e) {
            holder.tv_time.setText("已过期");
            e.printStackTrace();
        }
    }

    private class mRequestListener implements RequestListener {
        private ViewHolder holder;
        public mRequestListener (ViewHolder holder){
            this.holder = holder;
        }
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
            Log.d(TAG, "onException: " + e.toString()+"  model:"+model+" isFirstResource: "+isFirstResource);
            Message msg = new Message();
            msg.what = 0;
            msg.obj = holder.iv_image;
            mHandler.sendMessage(msg);
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
            Log.e(TAG,  "model:"+model+" isFirstResource: "+isFirstResource);
            return false;
        }
    };

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    RoundedCorners roundedCorners = new RoundedCorners(1);
                    RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(158, 130);
                    Glide.with(mContext).asBitmap().load(R.mipmap.gift_bg).apply(options).into((ImageView)msg.obj);
                    break;
            }
        }
    };

    @Override
    public int getItemCount() {
        return doHomeWorkListBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_music, tv_time, tv_createdate, tv_do;
        ImageView iv_image, iv_music, iv_type;
        View homeWorkView;
        LinearLayout llBg, llComment;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);//标题
            iv_image = itemView.findViewById(R.id.iv_image);//左边图片
            iv_music = itemView.findViewById(R.id.iv_music);//录音还是音乐  图片不一样
            tv_music = itemView.findViewById(R.id.tv_music);//录音还是音乐
            tv_time = itemView.findViewById(R.id.tv_time);//3天
            tv_createdate = itemView.findViewById(R.id.tv_createdate);//创建时间
            tv_do = itemView.findViewById(R.id.tv_do);
            iv_type = itemView.findViewById(R.id.iv_type);
            llBg = itemView.findViewById(R.id.ll_bg);
            llComment = itemView.findViewById(R.id.ll_comment);
            this.homeWorkView = itemView;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getLayoutPosition();
                    if (null != doHomeWorkListBeanList.get(position).getMfTpLRR()) {
                        if (!StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpLRR().getType())) {
                            if (doHomeWorkListBeanList.get(position).getMfTpLRR().getType().equals("3")){
                                //录音又分未完成和已完成
                                if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack(position, 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                } else {
                                    if (doHomeWorkListBeanList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack(position, 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    } else {
                                        listener.callBack(position, 4);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    }
                                }
                            }else if(doHomeWorkListBeanList.get(position).getMfTpLRR().getType().equals("4")){
                                listener.callBack(position, 2);//直接去视频
                            }else {
                                if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack(position, 0);//创作 0  磨耳朵1  阅读2  录音3
                                } else {
                                    if (doHomeWorkListBeanList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack(position, 0);//创作 0  磨耳朵1  阅读2  录音3
                                    } else {
                                        listener.callBack(position, 1);//创作 0  磨耳朵1  阅读2  录音3
                                    }
                                }
                            }
                        }
                    }else {
                        if (null != doHomeWorkListBeanList.get(position).getMfVideo()){
                            listener.callBack(position, 2);//直接去视频
                        }else {
                            if (null != doHomeWorkListBeanList.get(position).getMfTpCreation()) {
                                if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack(position, 0);//创作 0  磨耳朵1  阅读2  录音3
                                } else {
                                    if (doHomeWorkListBeanList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack(position, 0);//创作 0  磨耳朵1  阅读2  录音3
                                    } else {
                                        listener.callBack(position, 1);//创作 0  磨耳朵1  阅读2  录音3
                                    }
                                }
                            } else {
                                //录音又分未完成和已完成
                                if (StringUtils.isEmpty(doHomeWorkListBeanList.get(position).getMfTpRecordFlag())) {
                                    listener.callBack(position, 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                } else {
                                    if (doHomeWorkListBeanList.get(position).getMfTpRecordFlag().equals("0")) {
                                        listener.callBack(position, 3);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    } else {
                                        listener.callBack(position, 4);//创作 0  磨耳朵1  阅读2  录音3 未完成  4已完成
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    }
}
